package com.cwf.metadatamanage.utils;

import com.cwf.metadatamanage.enums.HttpCodeEnum;
import com.cwf.metadatamanage.model.vo.ResultResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.List;

public class BindingResultUtils {

    /**
     * 统一处理参数错误
     *
     * @param bindingResult
     * @return
     */
    public static ResponseEntity returnBindingResultError(BindingResult bindingResult) {
        List<FieldError> fieldErrorList = bindingResult.getFieldErrors();
        StringBuilder stringBuilder = new StringBuilder();
        fieldErrorList.forEach(v -> {
            stringBuilder.append(v.getDefaultMessage()).append(";");
        });
        ResultResponse resultResponse = new ResultResponse().fail(HttpCodeEnum.FAIL.getCode(), stringBuilder.toString());
        return ResponseEntity.status(resultResponse.getCode()).body(resultResponse);
    }
}
